<?php
/*
 * $Id: ActionFormTest.class.php 157 2009-12-16 15:59:18Z namba0219 $
 * =============================================================================
 * Barge - web application framework on PHP5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace barge\web;

use \UnitTestCase;

use barge\System;
use barge\utils\ArrayList;
use barge\utils\HashMap;
use barge\utils\IndexOutOfBoundsException;
use barge\utils\IllegalAccessException;
use barge\utils\IllegalArgumentException;
use barge\web\Validator;
use barge\web\Messages;
use barge\web\FormFile;


/**
 * @see UnitTestCase
 */
class ActionFormTest extends UnitTestCase
{
	/**
	 * @test
	 */
	public function testReset()
	{

	}

	/**
	 * @test
	 */
	public function testPopulate()
	{
		$form = new _GenericActionForm();
		$form->populate(new HashMap(array('age' => 30)));
		$this->assertTrue($form->getAge() === 30);
		// 文字列型
		$form->populate(new HashMap(array('name' => 'apple')));
		$this->assertIdentical($form->getName(), 'apple');
		// 数値への変換
		$form->populate(new HashMap(array('age' => '20')));
		$this->assertIdentical($form->getAge(), 20);

		// 数値への変換(2)
		$form->populate(new HashMap(array('age' => 'aaa')));
		$this->assertIdentical($form->getAge(), 0);
		// bool型への変換
		$form->populate(new HashMap(array('selected' => '1')));
		$this->assertIdentical($form->isSelected(), true);

		// リストで単一値を受け取る
		$form->populate(new HashMap(array('list' => 'abc')));
		$this->assertIdentical($form->getList()->size(), 0);
		$form->populate(new HashMap(array('list' => array('abc'))));
		$this->assertIdentical($form->getList()->size(), 1);
		$this->assertIdentical($form->getList()->get(0), 'abc');

		// リストで既にあるオブジェクトに値を設定する（オブジェクト）
		$form = new _GenericActionForm();
		$list = new ArrayList();
		$list->add(new _ListItem('apple'));
		$list->add(new _ListItem('orange'));
		$form->setList($list);
		$form->populate(new HashMap(array('list' => array(array('selected' => '1')))));	// list[0][selected]=1
		$this->assertIdentical($form->getList()->size(), 2);
		$this->assertIsA($form->getList()->get(0), 'barge\web\_ListItem');
		$this->assertIdentical($form->getList()->get(0)->getSelected(), true);

		// リストで既にあるオブジェクトに値を設定する（オブジェクト、タイプヒンティングなし）
		$form = new _GenericActionForm();
		$list = new ArrayList();
		$list->add(new _ListItem('apple'));
		$list->add(new _ListItem('orange'));
		$form->setList2($list);
		$form->populate(new HashMap(array('list2' => array(array('selected' => '1')))));	// list[0][selected]=1
		$this->assertIdentical($form->getList2()->size(), 2);
		$this->assertIsA($form->getList2()->get(0), 'barge\web\_ListItem');
		$this->assertIdentical($form->getList2()->get(0)->getSelected(), true);

		// リストで既にあるHashMapに値を設定する（タイプヒンティング）
		$form = new _GenericActionForm();
		$list = new ArrayList();
		$list->add(new HashMap(array('name'=>'apple', 'selected'=>false)));
		$list->add(new HashMap(array('name'=>'orange', 'selected'=>false)));
		$form->setList($list);
		$form->populate(new HashMap(array('list' => array(array('selected' => '1')))));	// list[0][selected]=1
		$this->assertIsA($form->getList(), 'barge\utils\ArrayList');
		$this->assertIdentical($form->getList()->size(), 2);
		$this->assertIsA($form->getList()->get(0), 'barge\utils\HashMap');
		$this->assertIdentical($form->getList()->get(0)->get('selected'), true);

		// タイプヒンティングから型の再現（ArrayList）
		$form = new _GenericActionForm();
		$form->populate(new HashMap(array('list' => array('apple', 'orange'))));	// list[0][selected]=1
		$this->assertIsA($form->getList(), 'barge\utils\ArrayList');
		$this->assertIdentical($form->getList()->size(), 2);
		$this->assertIdentical($form->getList()->get(0), 'apple');
		$this->assertIdentical($form->getList()->get(1), 'orange');

		// タイプヒンティングから型の再現（HashMap）
		$form = new _GenericActionForm();
		$form->populate(new HashMap(array('map' => array('apple' => '1', 'orange' => '0'))));	// list[0][selected]=1
		$this->assertIsA($form->getMap(), 'barge\utils\HashMap');
		$this->assertIdentical($form->getMap()->get('apple'), '1');
		$this->assertIdentical($form->getMap()->get('orange'), '0');

		// アップロード
		$form = new _UploadActionForm();
		$form->populate(new HashMap(array('file1' =>
			array( 'fileName' => 'file.gif' ))));
		$this->assertTrue($form->getFile1() instanceof FormFile);

		// TODO :
//		// パブリックプロパティー
//		$form = new _GenericActionForm();
//		$form->populate(new HashMap(array('address' => 'New York')));
//		$this->assertIdentical($form->address, 'New York');
	}

}

class _GenericActionForm extends ActionForm
{
	private $age = 0;
	private $name = "";
	private $selected = false;
	private $list;
	private $list2;
	private $map;
	public $address;

	/**
	 * @return string
	 */
	public function getName()
	{
		return $this->name;
	}

	public function setName($value)
	{
		$this->name = $value;
	}

	/**
	 * @return HashMap
	 */
	public function getMap()
	{
		return $this->map;
	}

	public function setMap(HashMap $value)
	{
		$this->map = $value;
	}

	/**
	 * @return ArrayList
	 */
	public function getList()
	{
		return $this->list;
	}

	public function setList(ArrayList $value)
	{
		$this->list = $value;
	}

	/**
	 * @return ArrayList
	 */
	public function getList2()
	{
		return $this->list2;
	}

	public function setList2($value)
	{
		$this->list2 = $value;
	}

	public function isSelected()
	{
		return $this->selected;
	}

	public function setSelected($value)
	{
		$this->selected = $value;
	}

	public function getAge()
	{
		return $this->age;
	}

	public function setAge($value)
	{
		$this->age = $value;
	}
}

class _ListItem
{
	private $name;
	private $selected = false;

	public function __construct($name)
	{
		$this->name = $name;
	}

	public function getName()
	{
		return $this->name;
	}

	public function setName($value)
	{
		$this->name = $value;
	}

	public function getSelected()
	{
		return $this->selected;
	}

	public function setSelected($value)
	{
		$this->selected = $value;
	}
}

class _UploadActionForm extends ActionForm
{
	private $file1;

	/**
	 * @return FormFile
	 */
	public function getFile1()
	{
		return $this->file1;
	}

	public function setFile1(FormFile $value)
	{
		$this->file1 = $value;
	}
}

?>